/* Copyright - Apache License 2.0
 * 
 * The project "kyou" is
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *      http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package net.kyou.data;

import org.junit.Assert;
import org.junit.Test;

public class DataDocumentTest {

    @Test
    public void testGetSet() {
        DataDocument doc = new DataDocument();

        Assert.assertNull(doc.get("a"));
        doc.map.put("a", "a");
        Assert.assertEquals("a", doc.get("a"));
    }

    @Test
    public void testChildren() {
        DataDocument doc = new DataDocument();
        doc.map.put("a", "a");
        doc.map.put("array", "");
        doc.map.put("array[]", "2");
        doc.map.put("array[0]", "aaa");
        doc.map.put("array[1]", "bbb");
        doc.map.put("stru.a", "aaa");
        doc.map.put("stru.b", "bbb");
        doc.map.put("stru.c", "ccc");

        Assert.assertNull(doc.elements("asdf"));
        Assert.assertArrayEquals(new String[] { "array[0]", "array[1]" }, doc.elements("array"));
        Assert.assertNull(null, doc.elements("stru.a"));
    }

    @Test
    public void testEntries() {
        DataDocument doc = new DataDocument();
        doc.map.put("a", "a");
        doc.map.put("array", "");
        doc.map.put("array[]", "2");
        doc.map.put("array[0]", "aaa");
        doc.map.put("array[1]", "bbb");

        Assert.assertArrayEquals(new String[] { "a", "array", "array[]", "array[0]", "array[1]" }, doc.map.keySet().toArray());
    }

    @Test
    public void testToString() {
        DataDocument doc = new DataDocument();
        doc.map.put("a", "a");
        doc.map.put("array", "");
        doc.map.put("array[]", "2");
        doc.map.put("array[0]", "aaa");
        doc.map.put("array[1]", "bbb");

        String xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n<data>\r\n<a>a</a>\r\n<array></array>\r\n<array[]>2</array[]>\r\n<array[0]>aaa</array[0]>\r\n<array[1]>bbb</array[1]>\r\n</data>";

        Assert.assertEquals(xml, doc.toString());
    }

}
